﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Eniux.Windows.ThreeDEngine.AttachObject;
using System.Windows.Media.Media3D;
using Eniux.Windows.ThreeDEngine.Visual3Ds;
using Eniux.Windows.ThreeDEngine.Helper;

namespace _3DTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            RectangleVisual3D rec = new RectangleVisual3D
            { 
                Material = MaterialHelper.CreateImageMaterial("tr.jpg"),
                Width=100,
                Length=100,
                DivLength=100,
                DivWidth=100,
                Origin=new Point3D(0,0,-0.1)
            };
            this.port.Children.Add(rec);
            //p1 = new Point3DCollection();
            //p1.Add(new Point3D(0, 0, 0));
            //p1.Add(new Point3D(3, 3, 0));
            //build(p1);
            //p2 = new Point3DCollection();
            //p2.Add(new Point3D(3, 3, 0));
            //p2.Add(new Point3D(4,5, 0));
            //build(p2);

            //QuadrilateralVisual3D qv = new QuadrilateralVisual3D() { Material = MaterialHelper.CreateImageMaterial("2.jpg") };
            //qv.Point1 = new Point3D(p2[0].X +1.1, p2[0].Y , p2[0].Z);
            //qv.Point2 = new Point3D(p2[0].X - 0.7, p2[0].Y + 0.9, p2[0].Z);
            //qv.Point3 = new Point3D(p2[0].X - 1.1, p2[0].Y+0.3 , p2[0].Z);
            //qv.Point4 = new Point3D(p2[0].X +0.35, p2[0].Y - 1.04, p2[0].Z);
            ////qv.Point1 = new Point3D(2, 2,0);
            ////qv.Point2 = new Point3D(2, 4, 0);
            ////qv.Point3 = new Point3D(5,8, 0);
            ////qv.Point4 = new Point3D(2, 4, 0);
            //p3 = new Point3DCollection();
            //p3.Add(new Point3D(4, 5, 0));
            //p3.Add(new Point3D(12, 13, 0));
            //build(p3);
          //  this.port.Children.Add(qv);
            build(GetRandomTopographyPoints());
            build(GetRandomTopographyPoints());

            DataContext = this;
        }



        void build(Point3DCollection p)
        {

          for(int i=0;i<p.Count-3;i++)
            {
               Point3DCollection tmp=new Point3DCollection();
               tmp.Add(p[i]);
               tmp.Add(p[i+1]);
               LinesVisual3D line = new LinesVisual3D() { Color = Colors.Gray, Thickness = 3, Points = tmp };
                line.HitElement3D.MouseEnter += new MouseEventHandler(HitElement3D_MouseEnter1);
                line.HitElement3D.MouseLeave += new MouseEventHandler(HitElement3D_MouseLeave1);
                PipeVisual3D box = new PipeVisual3D() { Point1 = new Point3D(p[i].X, p[i].Y, p[i].Z + 0.01), Point2 = new Point3D(p[i].X, p[i].Y, p[i].Z - 0.01), Diameter = 0.5 };

                box.HitElement3D.MouseEnter += new MouseEventHandler(HitElement3D_MouseEnter);
                box.HitElement3D.MouseLeave += new MouseEventHandler(HitElement3D_MouseLeave);
                this.port.Add(box);
                this.port.Add(line);
            } 
            //Vector3D  v3d=p[0]-p[1];
            //RectangleVisual3D rv = new RectangleVisual3D() 
            //{ 
            //    Origin = new Point3D((p[0].X+p[1].X)/2,(p[0].Y+p[1].Y)/2,(p[0].Z+p[1].Z)/2), Width = 2, Length = v3d.Length-1, Normal = new Vector3D(0, 0, 1),
            //    LengthDirection = v3d,
            //    Material= MaterialHelper.CreateImageMaterial("2.jpg")
            //};

          //  this.port.Add(rv);
            
        }

        void HitElement3D_MouseLeave(object sender, MouseEventArgs e)
        {
            PipeVisual3D parent = VisualTreeHelper.GetParent(sender as ModelUIElement3D) as PipeVisual3D;
            parent.Material = MaterialHelper.CreateMaterial(Brushes.Gray);
        }

        void HitElement3D_MouseEnter(object sender, MouseEventArgs e)
        {
            PipeVisual3D parent = VisualTreeHelper.GetParent(sender as ModelUIElement3D) as PipeVisual3D;
            parent.Material = MaterialHelper.CreateMaterial(Brushes.Red);
        }

        void HitElement3D_MouseLeave1(object sender, MouseEventArgs e)
        {
            LinesVisual3D parent = VisualTreeHelper.GetParent(sender as ModelUIElement3D) as LinesVisual3D;
            parent.Color = Colors.Gray;
        }

        void HitElement3D_MouseEnter1(object sender, MouseEventArgs e)
        {
            LinesVisual3D parent = VisualTreeHelper.GetParent(sender as ModelUIElement3D) as LinesVisual3D;
            parent.Color = Colors.Red;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
        }


        //public Point3DCollection p1;
        //public Point3DCollection p2;
        //public Point3DCollection p3;

        private Point3DCollection GetRandomTopographyPoints()
        {
            //create a 10x10 topography.
            Point3DCollection points = new Point3DCollection();
            Random r = new Random();
            double x;
            double y;
            double denom = 1000;
            int count = 0;
            for (int i = 0; i< 6; i++)
            {
                //for (int j = 0; j < 10;j++)
                //{
                    System.Threading.Thread.Sleep(1);
                    x = Convert.ToDouble(r.Next(-50, 50));
                    y = Convert.ToDouble(r.Next(-50, 50));/// denom;
                    points.Add(new Point3D(x, y, 0));
                   
                //}
            }
            return points;
        }

    }
}
